package mx.itesm.ccm.compu.dao;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class ContextHelper{

	private ApplicationContext context;
	private static ContextHelper INSTANCE;
	
	public static void setContext(ServletContext ctx){
		
		if(INSTANCE==null){
			INSTANCE = new ContextHelper(ctx);
		}		
	}
	
    private ContextHelper(ServletContext ctx) {
        ServletContext servletContext = ctx;
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    }
	
	public static Object getBean(String beanRef){
		
		if(INSTANCE!=null){
			return INSTANCE.context.getBean(beanRef);
		}
		else{
			return null;
		}
	}
    
}
